﻿
Imports System.Runtime.CompilerServices
Imports System.Text

Public Module _strcmp
    <Extension()> Public Function charsame(ByVal c1 As Char, ByVal c2 As Char, Optional ByVal carecase As Boolean = True) As Boolean
        If carecase Then
            Return c1 = c2
        Else
            Return Char.ToLower(c1) = Char.ToLower(c2)
        End If
    End Function

    <Extension()> Public Function strcmp(ByVal input1 As String, ByVal start1 As Int64, ByVal len1 As Int64,
                                         ByVal input2 As String, ByVal start2 As Int64, ByVal len2 As Int64,
                                         Optional ByVal carecase As Boolean = True) As Int64
        If carecase Then
            Return String.Compare(strmid(input1, start1, len1),
                                  strmid(input2, start2, len2))
        Else
            Return String.Compare(strtolower(strmid(input1, start1, len1)),
                                  strtolower(strmid(input2, start2, len2)))
        End If
    End Function

    <Extension()> Public Function strcmp(ByVal input1 As String, ByVal start1 As Int64,
                                         ByVal input2 As String, ByVal start2 As Int64, ByVal len As Int64,
                                         Optional ByVal carecase As Boolean = True) As Int64
        Return strcmp(input1, start1, len, input2, start2, len, carecase)
    End Function

    <Extension()> Public Function strcmp(ByVal s1 As String, ByVal s2 As String, ByVal len As Int64,
                                         Optional ByVal carecase As Boolean = True) As Int64
        Return strcmp(s1, 0, len, s2, 0, len, carecase)
    End Function

    <Extension()> Public Function strcmp(ByVal s1 As String, ByVal s2 As String,
                                         Optional ByVal carecase As Boolean = True) As Int64
        Return strcmp(s1, 0, strlen(s1), s2, 0, strlen(s2), carecase)
    End Function

    <Extension()> Public Function strcmp(ByVal s1 As StringBuilder, ByVal start1 As Int64, ByVal len1 As Int64,
                                         ByVal s2 As StringBuilder, ByVal start2 As Int64, ByVal len2 As Int64,
                                         Optional ByVal carecase As Boolean = True) As Int64
        Return strcmp(strmid(s1, start1, len1), strmid(s2, start2, len2), carecase)
    End Function

    <Extension()> Public Function strcmp(ByVal s1 As StringBuilder, ByVal start1 As Int64, ByVal len1 As Int64,
                                         ByVal s2 As String, ByVal start2 As Int64, ByVal len2 As Int64,
                                         Optional ByVal carecase As Boolean = True) As Int64
        Return strcmp(strmid(s1, start1, len1), 0, s2, start2, len2, carecase)
    End Function

    <Extension()> Public Function strcmp(ByVal s1 As String, ByVal start1 As Int64, ByVal len1 As Int64,
                                         ByVal s2 As StringBuilder, ByVal start2 As Int64, ByVal len2 As Int64,
                                         Optional ByVal carecase As Boolean = True) As Int64
        Return -strcmp(s2, start2, len2, s1, start1, len1, carecase)
    End Function

    <Extension()> Public Function strsame(ByVal input1 As String, ByVal start1 As Int64,
                                          ByVal input2 As String, ByVal start2 As Int64, ByVal len As Int64,
                                          Optional ByVal carecase As Boolean = True) As Boolean
        If len = 0 Then
            Return True
        ElseIf String.IsNullOrEmpty(input1) AndAlso String.IsNullOrEmpty(input2) Then
            Return True
        ElseIf String.IsNullOrEmpty(input1) OrElse String.IsNullOrEmpty(input2) Then
            Return False
        ElseIf start1 >= input1.Length OrElse start2 >= input2.Length Then
            Return False
        Else
            Return strcmp(input1, start1, len, input2, start2, len, carecase) = 0
        End If
    End Function

    <Extension()> Public Function strsame(ByVal input1 As String, ByVal start1 As Int64, ByVal len1 As Int64,
                                          ByVal input2 As String, ByVal start2 As Int64, ByVal len2 As Int64,
                                          Optional ByVal carecase As Boolean = True) As Boolean
        If len1 <> len2 Then
            Return False
        Else
            Return strsame(input1, start1, input2, start2, len1, carecase)
        End If
    End Function

    <Extension()> Public Function strsame(ByVal input1 As String, ByVal input2 As String, ByVal len As Int64,
                                          Optional ByVal carecase As Boolean = True) As Boolean
        Return strsame(input1, 0, input2, 0, len, carecase)
    End Function

    <Extension()> Public Function strsame(ByVal input1 As String, ByVal len1 As Int64,
                                          ByVal input2 As String, ByVal len2 As Int64,
                                          Optional ByVal carecase As Boolean = True) As Boolean
        Return strsame(input1, 0, len1, input2, 0, len2, carecase)
    End Function

    <Extension()> Public Function strsame(ByVal input1 As String, ByVal input2 As String,
                                          Optional ByVal carecase As Boolean = True) As Boolean
        Return strsame(input1, 0, strlen(input1), input2, 0, strlen(input2), carecase)
    End Function

    <Extension()> Public Function strendwith(ByVal s1 As String, ByVal s2 As String,
                                             Optional ByVal carecase As Boolean = True) As Boolean
        Return strsame(s1, strlen(s1) - strlen(s2), s2, 0, strlen(s2), carecase)
    End Function

    <Extension()> Public Function strstartwith(ByVal s1 As String, ByVal s2 As String,
                                               Optional ByVal carecase As Boolean = True) As Boolean
        Return strsame(s1, 0, s2, 0, strlen(s2), carecase)
    End Function

    <Extension()> Public Function str_empty_or_whitespaces(ByVal s As String) As Boolean
        If String.IsNullOrEmpty(s) Then
            Return True
        Else
            For i As Int32 = 0 To strlen(s) - 1
                If Not s(i).space() Then
                    Return False
                End If
            Next
            Return True
        End If
    End Function
End Module
